<?php


namespace App\Http\Controllers\Backend;

use App\Businesses\BusinessState;
use App\Constant\ApiV2Constant;
use App\Http\Controllers\Api\BaseController;
use App\Http\Requests\Company\CommentRequest;
use App\Services\Business\Interfaces\CommentServiceInterface;
use App\Services\Member\Interfaces\UserServiceInterface;
use App\Services\Member\Models\User;

use App\Services\Business\Models\Comment;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CommentController extends BaseController
{


    protected $commentService;
    protected $userService;
    protected $businessState;


    public function __construct(
        CommentServiceInterface $commentService,
        UserServiceInterface    $userService,
        BusinessState           $businessState
    )
    {
        $this->commentService = $commentService;
        $this->userService = $userService;
        $this->businessState = $businessState;

    }

    public function index(Request $request)
    {


        $createdAt = $request->input('created_at');
        $userKeyWords = $request->input('user_keywords');
        $scene = $request->input('scene');
        $keywords = $request->input('keywords');
        $userIds = [];
        if ($userKeyWords) {
            $userIds = User::query()
                ->where('nick_name', 'like', '%' . $userKeyWords . '%')
                ->orWhere('mobile', $userKeyWords)
                ->orWhere('uuid', $userKeyWords)
                ->select(['id'])->get()->pluck('id')->toArray();
        }
        $query = Comment::query()
            ->with(['user:id,nick_name,avatar,thumb,mobile'])
            ->when($keywords, function ($query) use ($keywords) {
                $query->where('content', 'like', '%' . $keywords . '%');
            })
            ->when($createdAt && is_array($createdAt), function ($query) use ($createdAt) {
                $query->whereBetween('created_at', [Carbon::parse($createdAt[0]), Carbon::parse($createdAt[1])]);
            })
            ->when($userIds, function ($query) use ($userIds) {
                $query->whereIn('user_id', $userIds);
            });
        if ($scene === 'reply') {
            $query->orderByDesc('children_count');
        } elseif ($scene === 'thumbsup') {
            $query->orderByDesc('thumbsup_count');
        } else {
            $query->orderByDesc('created_at');
        }
        $comments = $query->paginate($request->input('size', 10));
        return $this->successData(compact('comments'));
    }


    /**
     *
     * 资源讨论列表
     * @param Request $request
     * @param $type
     * @param $id
     * @return JsonResponse
     */
    public function comments(Request $request, $type, $id)
    {
        $size = $request->input('size', 10);
        $modal = 'App\Services\Business\Models\\' . ucfirst($type);
        $modal = new $modal;
        $resource = $modal::find($id);
        $comments = $resource->comments()->orderbyDesc('created_at')->where('parent_id', 0)->paginate($size);
        $commentUsers = $this->userService->getList(array_column($comments->items(), 'user_id'), ['role:id,name']);
        $commentUsers = arr2_clear($commentUsers, ApiV2Constant::MODEL_MEMBER_FIELD);
        $commentUsers = array_column($commentUsers, null, 'id');

        return $this->data([
            'comments' => $comments,
            'users' => $commentUsers,
        ]);
    }

    /**
     * 回复讨论
     * @param CommentRequest $request
     * @param string $type
     * @param int $id
     * @return JsonResponse
     */
    public function submitComment(CommentRequest $request, string $type, int $id)
    {

        $data = $request->filldata();
        $comment = $this->commentService->create($type, $id, $data);
        $comment_id = $comment['id'];
        $reply_id = $comment['reply_id'];
        return $this->data(compact('comment_id', 'reply_id'));
    }


    public function delete(Request $request, $id)
    {

        Comment::destroy($id);

        return $this->success(__('讨论删除成功'));
    }
}
